package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.DataGroupService;
import com.symone.webcms.model.DataGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 *   @description : 内容分组管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-25
 */
@RestController
@Api(value="/contentGroup", description="内容分组 管理")
@RequestMapping("/api/content/group")
public class DataGroupController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DataGroupController.class);

    @Autowired
    public DataGroupService dataGroupService;

    /**
     * @description : 获取内容分组分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取内容分组分页列表", notes="获取内容分组分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<DataGroup> ew = new EntityWrapper<DataGroup>();
        ew.where("deleted=0");

        //logger.debug("SQL:"+ew.originalSql());

        Page<DataGroup> page = new Page<DataGroup>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = dataGroupService.findPage(page, ew);

        return ResultMessage.success("获取内容分组的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有内容分组记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有内容分组信息", notes="获取所有内容分组信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有内容分组信息", dataGroupService.findAll());
    }

    /**
     * @description : 通过id获取内容分组
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取内容分组信息", notes="获取内容分组信息")
    @ApiImplicitParam(name="id",value="内容分组ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取内容分组信息成功!", dataGroupService.selectById(id));
    }

    /**
     * @description : 添加内容分组
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加内容分组信息", notes="添加内容分组信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "内容分组名称", required = true ),
            @ApiImplicitParam(name = "module", value = "模块名称", required = true )
    })
    public ResultMessage add(String name,String module){
        DataGroup dataGroup = new DataGroup();
        dataGroup.setLang(getSessionLanguage().getCode());
        dataGroup.setModule(module);
        dataGroup.setDeleted(0);
        dataGroup.setName(name);
        dataGroup.setCreateTime(new Date());
        dataGroupService.insertData(dataGroup);
        return ResultMessage.success("添加内容分组成功!",dataGroup);
    }

    /**
     * @description : 通过id逻辑删除内容分组
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除内容分组", notes="逻辑删除内容分组")
    @ApiImplicitParam(name = "id", value = "内容分组Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(dataGroupService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑内容分组删除成功！");
        } else {
            return ResultMessage.fail("逻辑内容分组删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除内容分组
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除内容分组", notes="物理删除内容分组")
    @ApiImplicitParam(name = "id", value = "内容分组Id", required = true )
    public ResultMessage remove(long id){
        if(dataGroupService.deleteById(id)) {
            return ResultMessage.success("物理删除内容分组成功！");
        } else {
            return ResultMessage.fail("物理删除内容分组失败！");
        }
    }

    /**
     * @description : 更新内容分组信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value="更新内容分组信息", notes="更新内容分组信息")
    @ApiImplicitParam(name = "内容分组", value = "内容分组信息", required = true )
    public ResultMessage update(DataGroup dataGroup) {
        if(dataGroupService.updateById(dataGroup)) {
            return ResultMessage.success("内容分组信息更新成功!");
        }else{
            return ResultMessage.fail("内容分组信息更新失败!");
        }
    }

    /**
    * @description : 清除内容分组记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
    */
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除内容分组记录", notes="清除已被逻辑删除的内容分组记录")
    public ResultMessage clear(){
        int num = dataGroupService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有内容分组记录被清除！");
        }
    }



}
